package com.cryptoneedle.garden.repository.source.metadata;


import com.bubbles.engine.data.core.repository.BaseRepository;
import com.cryptoneedle.garden.entity.source.metadata.SourceCatalog;
import com.cryptoneedle.garden.enums.SourceDatabaseType;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>description: 源数据源-目录 </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-20
 */
@Repository
public interface SourceCatalogRepository extends BaseRepository<SourceCatalog, String> {

    @Query("  FROM SourceCatalog " +
            "ORDER BY enabled desc, catalogName")
    List<SourceCatalog> catalogs();

    @Query("  FROM SourceCatalog " +
            "WHERE enabled = TRUE " +
            "ORDER BY enabled desc, catalogName")
    List<SourceCatalog> catalogsEnabled();

    @Query("FROM SourceCatalog WHERE dorisCatalogName = :dorisCatalogName")
    SourceCatalog catalogByDorisCatalogName(String dorisCatalogName);

    @Query("  FROM SourceCatalog " +
            "WHERE host = :host " +
            "  AND port = :port " +
            "ORDER BY enabled DESC, catalogName")
    List<SourceCatalog> catalogsByServer(String host, Integer port);

    @Query("  FROM SourceCatalog " +
            "WHERE host = :host " +
            "  AND port = :port " +
            "  AND databaseType = :databaseType " +
            "  AND route = :route " +
            "  AND userName = :userName " +
            "ORDER BY enabled DESC, catalogName")
    List<SourceCatalog> catalogsByJdbc(String host, Integer port, SourceDatabaseType databaseType, String route, String userName);
}